package com.ruoyi.web.controller.channel;

import com.ruoyi.channel.domain.MpChannel;
import com.ruoyi.channel.domain.MpChannelIp;
import com.ruoyi.channel.domain.MpChannelIpRSP;
import com.ruoyi.channel.domain.MpChannelMethodRSP;
import com.ruoyi.channel.service.IMpChannelIpService;
import com.ruoyi.channel.service.IMpChannelService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 *  信息操作处理
 * 
 * @author ruoyi
 * @date 2019-05-07
 */
@Controller
@RequestMapping("/channel/mpChannelIp")
public class MpChannelIpController extends BaseController
{
    private String prefix = "channel/mpChannelIp";
	
	@Autowired
	private IMpChannelIpService mpChannelIpService;
	@Autowired
	private IMpChannelService iMpChannelService;
	
	@GetMapping()
	public String mpChannelIp()
	{
	    return prefix + "/mpChannelIp";
	}
	
	/**
	 * 查询列表
	 */
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(MpChannelIp mpChannelIp)
	{
		startPage();
        List<MpChannelIp> list = mpChannelIpService.selectMpChannelIpList(mpChannelIp);
        List<MpChannelIpRSP> listRsp = new ArrayList<>();
		for (int i = 0; i < list.size(); i++) {
            MpChannelIpRSP mpChannelIpRSP = new MpChannelIpRSP();
            MpChannelIp channelIp = list.get(i);
            // ip白名单 id
            mpChannelIpRSP.setId(channelIp.getId());

            MpChannel mpChannel = iMpChannelService.selectMpChannelById(channelIp.getChannelId().intValue());
            // 渠道名称
            mpChannelIpRSP.setChannelName(mpChannel.getName());

            // 渠道标识
            mpChannelIpRSP.setChannelId(channelIp.getChannelId());

            // 白名单ip
            mpChannelIpRSP.setIp(channelIp.getIp());

            listRsp.add(mpChannelIpRSP);
        }
		return getDataTable(listRsp);
	}
	
	
	/**
	 * 导出列表
	 */
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MpChannelIp mpChannelIp)
    {
    	List<MpChannelIp> list = mpChannelIpService.selectMpChannelIpList(mpChannelIp);
		List<MpChannelIpRSP> listRsp = new ArrayList<>();
		for (int i = 0; i < list.size(); i++) {
			MpChannelIpRSP mpChannelIpRSP = new MpChannelIpRSP();
			MpChannelIp channelIp = list.get(i);
			// ip白名单 id
			mpChannelIpRSP.setId(channelIp.getId());

			MpChannel mpChannel = iMpChannelService.selectMpChannelById(channelIp.getChannelId().intValue());
			// 渠道名称
			mpChannelIpRSP.setChannelName(mpChannel.getName());

			// 渠道标识
			mpChannelIpRSP.setChannelId(channelIp.getChannelId());

			// 白名单ip
			mpChannelIpRSP.setIp(channelIp.getIp());

			listRsp.add(mpChannelIpRSP);
		}
        ExcelUtil<MpChannelIpRSP> util = new ExcelUtil<MpChannelIpRSP>(MpChannelIpRSP.class);
        return util.exportExcel(listRsp, "mpChannelIp");
    }
	
	/**
	 * 新增
	 */
	@GetMapping("/add/{channelId}")
	public String add(@PathVariable("channelId") Integer channelId, ModelMap mmap)
	{
		mmap.put("mpChannel",iMpChannelService.selectMpChannelById(channelId));
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存
	 */
	@Log(title = "渠道ip黑白名单", businessType = BusinessType.INSERT)
	@PostMapping("/addIp")
	@ResponseBody
	public AjaxResult addSave(MpChannelIp mpChannelIp)
	{		
		return toAjax(mpChannelIpService.insertMpChannelIp(mpChannelIp));
	}

	/**
	 * 修改
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap)
	{
		MpChannelIp mpChannelIp = mpChannelIpService.selectMpChannelIpById(id);
		mmap.put("mpChannelIp", mpChannelIp);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存
	 */
	@Log(title = "", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(MpChannelIp mpChannelIp)
	{		
		return toAjax(mpChannelIpService.updateMpChannelIp(mpChannelIp));
	}
	
	/**
	 * 删除
	 */
	@Log(title = "", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(mpChannelIpService.deleteMpChannelIpByIds(ids));
	}

	/**
	 * 	校验该渠道是否添加该IP
	 */
	@PostMapping("/checkIpUnique")
	@ResponseBody
	public String checkIpUnique(MpChannelIp mpChannelIp) {
		return mpChannelIpService.checkIpUnique(mpChannelIp);
	}

}
